package com.shanshan.crowd.service.impl;

import com.shanshan.crowd.entity.po.MemberPO;
import com.shanshan.crowd.entity.po.MemberPOExample;
import com.shanshan.crowd.mapper.MemberPOMapper;
import com.shanshan.crowd.service.api.MemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @ClassName MemberServiceImpl
 * @Author 山山大魔王
 * @Date 2022/7/28 17:51
 * @Version 1.0
 */
// @Transactional(readOnly = true) 针对查询做事务属性
@Transactional(readOnly = true)
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberPOMapper memberPOMapper;

    @Override
    public MemberPO getMemberPOByLoginAcct(String loginacct) {

        // 1.创建Example对象
        MemberPOExample example = new MemberPOExample();

        // 2.创建Criteria对象
        MemberPOExample.Criteria criteria = example.createCriteria();

        // 3.封装查询条件
        criteria.andLoginacctEqualTo(loginacct);

        // 4.执行查询
        List<MemberPO> memberPOList = memberPOMapper.selectByExample(example);

        // 5.获取结果
        if(memberPOList == null || memberPOList.size() == 0){
            return null;
        }
        return memberPOList.get(0);
    }


    @Transactional(
            propagation = Propagation.REQUIRES_NEW,
            rollbackFor = Exception.class,
            readOnly = false)
    @Override
    public void saveMember(MemberPO memberPO) {
        memberPOMapper.insertSelective(memberPO);
    }
}
